package com.epam.railway.jdbc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.railway.jdbc.model.Passenger;
import com.epam.railway.jdbc.transform.PassengerTransformer;


public class PassengerDao {
	
	private static final String QUERY_FOR_GET_ALL_USERS = "SELECT * FROM passenger";
	
	
	public static List<Passenger> getAllPassenger(HttpServletRequest request,
			HttpServletResponse response) {
		List<Passenger> listPassenger = null;
         try(Connection conn = ConnectionManager.getConnection()) {
           
  			PreparedStatement state = conn
					.prepareStatement(QUERY_FOR_GET_ALL_USERS);
			ResultSet rs = state.executeQuery();
			listPassenger = new ArrayList<Passenger>();
			listPassenger = PassengerTransformer.fromResultPassengerToList(rs);
			request.setAttribute("listPassenger", listPassenger);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return listPassenger;
	}

}
